//#########################################################
// 版权所有：中国农业银行软件开发中心
// 系统名称：移动办公应用-手机版
// 文件名称：loginReducer.js
// 创建时间：2016年06月28日
// 创建人员：Panda
// 功能说明：redux login with username and password reducer
//#########################################################

'use strict';

import * as ActionTypes from '../actions/types';

const initialState = {
    userInfo: '',
    logining: false,
    logined: false,
    validate: false,
    inputing: false,
};

function loginWithUsernamePassword(state = initialState, action) {
    state = Object.assign({}, state, {
        logined: false,
    });

    switch (action.type) {
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_LOGIN_START:
            return Object.assign({}, state, {
                logining: true,
            });
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_VALIDATE_FAILED:
            return Object.assign({}, state, {
                validate: false,
                logining: false,
                logined: false,
                inputing: false,
            });
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_LOGIN_SUCCESS:
            return Object.assign({}, state, {
                logining: false,
                logined: true,
                validate: true,
                inputing: false,
                userinfo: action.data,
                actionType: ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_LOGIN_SUCCESS,
            });
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_LOGIN_FALIED:
            return Object.assign({}, state, {
                logining: false,
                logined: false,
                validate: true,
                inputing: false,
                code: action.code,
                failedMsg: action.msg,
                actionType: ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_LOGIN_FALIED,
            });
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_CHANGE_LOGIN_AUTH:
            let newState = state;
            if (action.username != undefined && action.username.length >= 0) {
                newState = Object.assign({}, newState, {
                    username: action.username,
                    inputing: true,
                });
            }
            if (action.password != undefined && action.password.length >= 0) {
                newState = Object.assign({}, newState, {
                    password: action.password,
                    inputing: true,
                });
            }
            return newState;
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_CLEAR_PASSWORD:
            return Object.assign({}, state, {
                password: '',
            });
        case ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_CLEAR:
            return Object.assign({}, state, {
                userId: '',
                notesId: '',
                soiHubIp: '',
                ssoName: '',
                actionType: ActionTypes.LOGIN_LOGINWITHUSERNAMEPASSWORD_CLEAR,
            });
        case ActionTypes.LOGIN_CLEAR_LOGIN_FAILED_MSG:
            return Object.assign({}, state, {
                actionType: ActionTypes.LOGIN_CLEAR_LOGIN_FAILED_MSG,
                code: '',
                failedMsg: '',
            });
        case ActionTypes.LOGOUT_SUCCESS:
        case ActionTypes.LOGOUT_FAILED:
            return initialState;
        default:
            return state;
    }
}

module.exports = loginWithUsernamePassword;